package com.msk.sys.utils;



import com.aspose.words.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 功能：
 * 作者：Zhang_XinGang
 * 时间：2020/3/3 22:52
 */
public class AsposeClient {
    private String randomTemp;
    public void setRandomTemp(String temp){
        randomTemp = temp;
    }
    
    /**
     * 加载注册文件
     * @return
     */
    public boolean setWordLicense(){
        try {
            String classesPath = SystemPath.getJavaClassPath();
            File file = new File(classesPath + "license.xml");
            if(!file.exists()){
                file = new File("/"+classesPath + "license.xml");
            }
            InputStream is = new FileInputStream(file);

            License aposeLic = new License();
            aposeLic.setLicense(is);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * word添加图片水印
     * @param wordPath
     * @param imgPath
     * @return
     */
    public String wordInsertWatermark(String wordPath,String imgPath){

        String newDoc = null;
        try {
            setWordLicense();

            newDoc = getTemporary() + "/" + UUID.randomUUID().toString() + ".docx";

            Document doc = new Document(wordPath);

            Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
            // Set up the text of the watermark.
            //watermark.getTextPath().setText(watermarkText);
            //watermark.getTextPath().setFontFamily("Arial");


            watermark.getImageData().setImage(imgPath);

            //watermark.setWidth(500);
            //watermark.setHeight(100);
            // Text will be directed from the bottom-left to the top-right corner.
            //watermark.setRotation(-40);
            // Remove the following two lines if you need a solid black text.
            //watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
            //watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark
            // Place the watermark in the page center.
            watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
            watermark.setWrapType(WrapType.NONE);
            watermark.setVerticalAlignment(VerticalAlignment.CENTER);
            watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
            // Create a new paragraph and append the watermark to this paragraph.
            Paragraph watermarkPara = new Paragraph(doc);
            watermarkPara.appendChild(watermark);
            // Insert the watermark into all headers of each document section.
            for (Section sect : doc.getSections()) {
                // There could be up to three different headers in each section, since we want
                // the watermark to appear on all pages, insert into all headers.
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
            }
            doc.save(newDoc);
            Document doc1 = new Document(newDoc);
            if(doc1.getPageCount()<1){
                newDoc = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            newDoc = null;
        }

        return newDoc;
    }

    /**
     * 获取临时目录
     * @return
     */
    public String getTemporary(){
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
        String filePath = getFileTemporary() + format2.format(new Date()) + "/" + randomTemp;
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }
        return filePath;
    }

    public String getFileTemporary() {
        String classesPath = SystemPath.getJavaClassPath();
        String temporary = classesPath.replace("/WEB-INF/classes/","") + "/temporary/";
        File file = new File(temporary);
        if(!file.exists()){
            temporary = "/" + temporary;
        }
        return temporary;
    }

    /**
     * 单页添加水印可支持图片水印和文字水印
     * @param watermarkPara
     * @param sect
     * @param headerType
     * @throws Exception
     */
    private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception {
        HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null) {
            // There is no header of the specified type in the current section, create it.
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }
        // Insert a clone of the watermark into the header.
        header.appendChild(watermarkPara.deepClone(true));
    }
}
